<template>
  <div class="essay-container">
    <header class="header">
      <h1 class="title">Essay revision assistant</h1>
      <p class="subtitle">专业AI辅助，提升您的写作水平</p>
    </header>

    <div class="editor-section">
      <div class="input-area">
        <label for="essay-input" class="input-label">请输入您的作文：</label>
        <textarea id="essay-input" v-model="userEssay" placeholder="在此粘贴或输入您的作文内容..." class="essay-textarea"
          rows="12"></textarea>
        <div class="word-count">{{ userEssay.length }} 字</div>
      </div>

      <button @click="submitEssay" :disabled="loading || !userEssay.trim()" class="submit-btn">
        <span v-if="!loading">提交修改</span>
        <span v-else class="loading-spinner"></span>
      </button>
    </div>

    <div v-if="loading" class="loading-indicator">
      <div class="spinner"></div>
      <p>AI正在认真修改您的作文，请稍候...</p>
    </div>

    <div v-if="modifiedEssay || feedback" class="results-section">
      <div v-if="modifiedEssay" class="result-card">
        <div class="result-header">
          <h2>修改后的作文</h2>
          <span class="badge">AI优化版</span>
        </div>
        <div class="result-content" v-html="modifiedEssay"></div>
      </div>

      <div v-if="feedback" class="feedback-card">
        <h2>修改意见与建议</h2>
        <div class="feedback-content">
          <p>{{ feedback }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from "vue";
import getAiResponse from "../../api/getAi.js";
import { marked } from "marked";

const userEssay = ref("");
const modifiedEssay = ref("");
const feedback = ref("");
const loading = ref(false);

const submitEssay = async () => {
  if (!userEssay.value.trim()) {
    alert("请输入作文内容！");
    return;
  }

  loading.value = true;
  modifiedEssay.value = "";
  feedback.value = "";

  const messages = [
    {
      role: "user",
      content: `请修改以下作文：\n${userEssay.value},要求给出修改后的文章`,
    },
  ];

  try {
    const response = await getAiResponse(messages);
    console.log(response);

    if (response) {
      modifiedEssay.value = marked(response.content || "修改失败");
      // 假设反馈信息在response的另一个字段中
      feedback.value = response.feedback || "AI提供了优化建议，请查看修改后的作文。";
    } else {
      alert("获取修改结果失败，请稍后重试");
    }
  } catch (error) {
    console.error("修改请求失败:", error);
    alert("修改过程中出现错误，请重试");
  } finally {
    loading.value = false;
  }
};


import { inject } from "vue";

const theme = inject("theme");
const setTheme = (newTheme) => {
  theme.value = newTheme; // 更新全局主题
};
</script>

<style scoped>
.essay-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 2rem;
  font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  color: var(--btn-text);
}

.header {
  text-align: center;
  margin-bottom: 2.5rem;
}

.title {
  font-size: 2.2rem;
  font-weight: 600;
  /* color: var(--primary-color); */
  margin-bottom: 0.5rem;
}

.subtitle {
  font-size: 1.1rem;
  /* color: var(--text-secondary); */
  opacity: 0.8;
}

.editor-section {
  background: var(--bg-color);
  border-radius: 12px;
  padding: 1.5rem;
  box-shadow: 0 0px 13px var(--border-color);
  margin-bottom: 2rem;
}

.input-area {
  margin-bottom: 1.5rem;
}

.input-label {
  display: block;
  font-weight: 500;
  margin-bottom: 0.75rem;
  color: var(--text-primary);
}

.essay-textarea {
  width: 95%;
  padding: 1rem;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  font-size: 1rem;
  line-height: 1.6;
  resize: vertical;
  min-height: 200px;
  background: var(--input-bg);
  color: var(--text-primary);
  /* transition: border-color 0.3s, box-shadow 0.3s; */
  /* border: none; */
}

.essay-textarea:focus {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.1);
  outline: none;
}

.word-count {
  text-align: right;
  font-size: 0.85rem;
  color: var(--text-tertiary);
  margin-top: 0.5rem;
}

.submit-btn {
  display: block;
  width: 100%;
  padding: 0.85rem;
  background: var(--primary-color);
  /* color: white; */
  color: var(--btn-text);
  /* border: none; */
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.3s, transform 0.2s;

}

.submit-btn:hover:not(:disabled) {
  background: var(--primary-dark);
  transform: translateY(-1px);
}

.submit-btn:disabled {
  background: var(--disabled-bg);
  cursor: not-allowed;
  opacity: 0.7;
}

.loading-spinner {
  display: inline-block;
  width: 1.2rem;
  height: 1.2rem;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: white;
  animation: spin 1s ease-in-out infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.loading-indicator {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  text-align: center;
  color: var(--text-secondary);
}

.loading-indicator .spinner {
  width: 3rem;
  height: 3rem;
  border: 3px solid var(--border-color);
  border-radius: 50%;
  border-top-color: var(--primary-color);
  animation: spin 1s linear infinite;
  margin-bottom: 1rem;
}

.results-section {
  margin-top: 2rem;
}

.result-card,
.feedback-card {
  background: var(--card-bg);
  border-radius: 12px;
  padding: 1.5rem;
  margin-bottom: 1.5rem;
  box-shadow: 0 0px 13px var(--border-color);
}

.result-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.result-header h2 {
  font-size: 1.4rem;
  color: var(--text-primary);
  margin: 0;
}

.badge {
  background: var(--primary-light);
  color: var(--primary-color);
  padding: 0.25rem 0.75rem;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: 500;
}

.result-content {
  line-height: 1.8;
  color: var(--text-primary);
}

.result-content :deep(p) {
  margin-bottom: 1rem;
}

.result-content :deep(strong) {
  color: var(--primary-color);
}

.feedback-card h2 {
  font-size: 1.4rem;
  color: var(--text-primary);
  margin-top: 0;
  margin-bottom: 1rem;
}

.feedback-content {
  background: var(--feedback-bg);
  padding: 1rem;
  border-radius: 8px;
  line-height: 1.7;
}

.feedback-content p {
  margin: 0;
}

@media (max-width: 768px) {
  .essay-container {
    padding: 1rem;
  }

  .title {
    font-size: 1.8rem;
  }

  .editor-section {
    padding: 1rem;
  }
}
</style>